/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tomica.utils;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tran
 */
public class TomicaUtils {
    
    private static ByteBuffer buffer = ByteBuffer.allocate(8);
    
    public static byte[] longToBytes(long x) {
        System.out.println("long x: "+x);
        return ByteBuffer.allocate(8).putLong(x).array();
    }

    public static long bytesToLong(byte[] bytes) {
        buffer.put(bytes, 0, bytes.length);
        buffer.flip();//need flip 
        return buffer.getLong();
    }
    
    public static final String convertDateToString(Date date) {
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        return  df.format(date);
    }
    
    public static byte[] charsToBytes(char[] str) {
        try {
            return new String(str).getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            //Logger.getLogger(TomicaUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    
}
